package com.screwdriver.jso.processor.generator;

import com.screwdriver.jso.processor.utils.TypeResolver;
import com.screwdriver.jso.processor.utils.TypeResolver.Type;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA. User: jan kowalski Date: 12.08.11 Time: 20:49 To
 * change this template use File | Settings | File Templates.
 */
@SuppressWarnings("deprecation")
public class JSNIGenerator {

    private static Map<Type, Generator> generatorMap;

    static {
        generatorMap = new HashMap<Type, Generator>();

        generatorMap.put(Type.ENUM, new EnumGenerator());
        generatorMap.put(Type.DATE, new DateGenerator());
        generatorMap.put(Type.BIGDECIMAL, new BigDecimalGenerator());

        generatorMap.put(Type.INT, new PrimitiveGenerator());
        generatorMap.put(Type.BYTE, new PrimitiveGenerator());
        generatorMap.put(Type.BOOLEAN, new PrimitiveGenerator());

        generatorMap.put(Type.STRING, new StringConvertionGenerator());
        generatorMap.put(Type.JSO, new JsoConvertionGenerator());

    }

    public static String generateSetter(String propertyName, String parameterName, TypeResolver propertyType) {
        Generator generator = generatorMap.get(propertyType.getType());
        if (generator != null) {
            return "/*-{\n" +
                    "\tthis." + propertyName + "= " + generator.setter(parameterName, propertyType) + "\n" +
                    "}-*/";
        }
        return "/*-{}-*/";
    }

    public static String generateGetter(String propertyName, TypeResolver propertyType) {
        Generator generator = generatorMap.get(propertyType.getType());
        if (generator != null) {
            return "/*-{\n" +
                    "\treturn " + generator.getter("this." + propertyName, propertyType) + "\n " +
                    "}-*/";
        }
        return "/*-{}-*/";
    }
}
